
//liuweibo add start 20120326
package com.android.matmanual;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.os.SystemProperties;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;


public class VersionTest extends BaseActivity 
{
	
	TextView mVersion;
	TextView mKernelVersion;
	TextView mBasebandVersion;
	WakeLock mWakeLock;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
     
		setContentView(R.layout.version_test);
		setTitle(R.string.buildnumber_title);
		PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); 
		mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "XYTEST"); 
		mWakeLock.acquire(); 
	
		mVersion = (TextView) findViewById(R.id.version);				
		mVersion.setText(getResources().getString(R.string.buildnumber_version)+ Build.DISPLAY);	
		mKernelVersion = (TextView) findViewById(R.id.kernel_version);
		
		String kernelVerion = getFormattedKernelVersion();
        String subString;
        if(null != kernelVerion && !kernelVerion.equals("") && kernelVerion.indexOf("-") > 0)
		{
			subString = kernelVerion.substring(0,kernelVerion.indexOf("-"));
		}else{
			subString = kernelVerion;
		}
		mKernelVersion.setText(getResources().getString(R.string.kernel_version)+subString);
		
		mBasebandVersion = (TextView) findViewById(R.id.baseband_version);
		mBasebandVersion.setText(getResources().getString(R.string.baseband_version)+
				SystemProperties.get("gsm.version.baseband", ""));
	}

							
	private String getFormattedKernelVersion() {
        String procVersionStr;

        try {
            BufferedReader reader = new BufferedReader(new FileReader("/proc/version"), 256);
            try {
                procVersionStr = reader.readLine();
            } finally {
                reader.close();
            }

            final String PROC_VERSION_REGEX =
                "\\w+\\s+" + /* ignore: Linux */
                "\\w+\\s+" + /* ignore: version */
                "([^\\s]+)\\s+" + /* group 1: 2.6.22-omap1 */
                "\\(([^\\s@]+(?:@[^\\s.]+)?)[^)]*\\)\\s+" + /* group 2: (xxxxxx@xxxxx.constant) */
                "\\((?:[^(]*\\([^)]*\\))?[^)]*\\)\\s+" + /* ignore: (gcc ..) */
                "([^\\s]+)\\s+" + /* group 3: #26 */
                "(?:PREEMPT\\s+)?" + /* ignore: PREEMPT (optional) */
                "(.+)"; /* group 4: date */

            Pattern p = Pattern.compile(PROC_VERSION_REGEX);
            Matcher m = p.matcher(procVersionStr);

            if (!m.matches()) {
                  return "Unavailable";
            } else if (m.groupCount() < 4) {
                return "Unavailable";
            } else {
                return (new StringBuilder(m.group(1)).append("\n").append(
                        m.group(2)).append(" ").append(m.group(3)).append("\n")
                            .append(m.group(4))).toString();
                }
            } catch (IOException e) {  
 

                return "Unavailable";
        }
    }
	
	 @Override
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
		 	

		 	if(keyCode == KeyEvent.KEYCODE_BACK)
		 	{
				finish();
		 	}	 
		 	return true;
	 	}
	    @Override
	    protected void onDestroy() {
	    	// TODO Auto-generated method stub
			if(mWakeLock!=null)
			{
				mWakeLock.release(); 
				mWakeLock = null;
			}
	    	super.onDestroy();
	    }	 
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
        finish();
		super.onStop();
	}

	@Override
	public void onAttachedToWindow() 
	{
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
		this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
	}

	
}
//liuweibo end
		 